<?php 
		
		/*
		分类编辑递归
		 */
		function editCate($data,$cate=array()){
		// print_r($data);die;
		$pid = $data['pid'];
		//查找当前分类
		$sql = "select * from `cate` where `pid`=$pid";
		$result = mysqli_query($GLOBALS['link'],$sql);

		//循环将查询到的同级存储到重组数组中
		while($arr = mysqli_fetch_assoc($result)){
			$cate[$data['id']][]=$arr;//将查询到的同级数据存储到临时数组中

		}
		
		//如果没有找到顶级才去找父级
		if($pid!=0){
			//找父级
			$sql = "select * from `cate` where `id`=$pid";
			$result = mysqli_query($GLOBALS['link'],$sql);
			$data = mysqli_fetch_assoc($result);

			//调用自身找父级的同级
			$cate = editCate($data,$cate);
		}

		//将数组倒序
		return $cate = array_reverse($cate,true);
	}

	/*
	分类列表的递归
	 */
	function getCate($pid=0,$cate=array(),$level=0){
	$sql = "select * from `cate` where pid=$pid ";
	$result = mysqli_query($GLOBALS['link'],$sql);
	while($arr = mysqli_fetch_assoc($result)){
		$str = '';
		for ($i=0; $i <$level; $i++){
			$str.='★';

		}
		$arr['name'] = $str.$arr['name'];
		$cate[]=$arr;
		$cate = getCate($arr['id'],$cate,$level+1);
	}

	return $cate;
}

